package com.echo.code.thread.mode.active;

import com.echo.code.thread.future.Future;
import java.lang.reflect.Method;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

/**
 * ActiveMessage Created by baiyap on 2018/10/6.
 */
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class ActiveMessage {

  private Object service;
  private Object[] args;
  private Method method;
  private Future<Object> result;

  // 通过反射的方式调用执行的具体实现
  public void execute() {
    try {
      Object realResult = method.invoke(service, args);
      if (Objects.nonNull(result)) {
        Future realFuture = (Future) realResult;
        result.finish(realFuture.get());
      }
    } catch (Exception e) {
      if (Objects.nonNull(result)) {
        result.finish(null);
      }
    }
  }
}

